﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using digit.Core;
using SMS.ServiceManager;
using SMS.UserManager;

public partial class Modules_ListTheCity : System.Web.UI.UserControl
{
    protected int EditIndex = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.lnkDelete.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");
        this.lnkDeleteBottom.Attributes.Add("onclick", "return confirm('Bạn đã chắc chắn xóa?');");

        if (this.IsPostBack) return;

        if (ClientState.MemberInfo.UserType != 0 && ClientState.MemberInfo.UserType != 1)
        {
            lnkDelete.Enabled = false;
            lnkDeleteBottom.Enabled = false;
        }
        else
        {
            lnkDelete.Enabled = true;
            lnkDeleteBottom.Enabled = true;
        }

        this.BindData(-1);
    }

    private void BindData(int index)
    {
        try
        {
            DataTable dt = CityManager.SelectAll();
            DataView dv = dt.DefaultView;
            if (txtCityName.Text != "")
                dv.RowFilter = "CityName LIKE '%" + txtCityName.Text.Trim() + "%'";

            grvCity.EditIndex = index;
            grvCity.DataSource = dv;
            grvCity.DataBind();
            if (grvCity.Rows.Count > 0)
            {
                string confirm = "return confirm('Bạn có muốn xóa thông tin này?')";
                for (int i = 0; i < grvCity.Rows.Count; i++)
                {
                    GridViewRow row = grvCity.Rows[i];
                    Button lbtn = (Button)row.FindControl("lnkDelete");
                    if (lbtn != null)
                        lbtn.Attributes.Add("onclick", confirm);

                    DropDownList ddlCountry = (DropDownList)row.FindControl("ddlCountry");
                    if (ddlCountry != null)
                        ddlCountry.SelectedValue = dt.Rows[i]["CountryID"].ToString();

                    DropDownList ddlEditCountry = (DropDownList)row.FindControl("ddlEditCountry");
                    if (ddlEditCountry != null)
                        ddlEditCountry.SelectedValue = dt.Rows[i]["CountryID"].ToString();
                }
            }
        }
        catch { }
    }

    protected void grvCity_RowEditing(object sender, GridViewEditEventArgs e)
    {
        EditIndex = e.NewEditIndex;
        BindData(EditIndex);
    }

    protected void grvCity_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        BindData(-1);
    }

    protected void grvCity_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        try
        {
            int id = e.RowIndex;
            grvCity.EditIndex = id;
            GridViewRow row = grvCity.Rows[id];

            string strEditCityName = ((TextBox)row.FindControl("txtEditCityName")).Text;
            string strEditCityCode = ((TextBox)row.FindControl("txtEditCityCode")).Text;
            byte bytEditCountry = byte.Parse(((DropDownList)row.FindControl("ddlEditCountry")).SelectedValue);

            int intID = int.Parse(grvCity.DataKeys[id].Value.ToString());
            if (intID > 0)
            {
                if (strEditCityName == "")
                {
                    return;
                }
                else
                {
                    CityInfo objInfo = new CityInfo();

                    objInfo.CountryID = intID;
                    objInfo.CityName = strEditCityName;
                    objInfo.CityCode = strEditCityCode;

                    CityManager.Update(objInfo);
                }
            }
            BindData(-1);
        }
        catch { }
    }

    protected void grvCity_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            string commandName = e.CommandName;
            GridViewRow row = grvCity.FooterRow;
            if (commandName == "Insert")
            {
                string strInsCityName = ((TextBox)row.FindControl("txtInsCityName")).Text;
                string strInsCityCode = ((TextBox)row.FindControl("txtInsCityCode")).Text;
                int intInsCountryID = int.Parse(((DropDownList)row.FindControl("ddlInsCountry")).SelectedValue);

                if (strInsCityName == "")
                {
                    return;
                }
                else
                {
                    CityInfo objInfo = new CityInfo();

                    objInfo.CityName = strInsCityName;
                    objInfo.CityCode = strInsCityCode;
                    objInfo.CountryID = intInsCountryID;

                    CityManager.Insert(objInfo);
                    BindData(-1);
                }
            }
        }
        catch { }
    }

    protected void grvCity_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            int intID = (int)grvCity.DataKeys[e.RowIndex].Value;
            if (intID != 0)
            {
                CityManager.Delete(intID);
            }
            //Reload.
            BindData(-1);
        }
        catch { }
    }

    protected void lnkDelete_Click(object sender, EventArgs e)
    {
        int iDeleted = 0;

        //Search all checked rows to delete
        foreach (GridViewRow r in this.grvCity.Rows)
        {
            //If checkbox is not checked then quit
            HtmlInputCheckBox cbx = (HtmlInputCheckBox)r.Cells[0].Controls[1];
            if (!cbx.Checked) continue;

            //If checkbox is checked then delete
            CityManager.Delete(Util.ToInteger(cbx.Value));

            iDeleted++;
        }

        //Display result of delete process
        if (iDeleted == 0)
        {
            this.lblError.Text = "Không có bản ghi nào được xóa!";
            return;
        }

        this.lblError.Text = "Đã xóa " + iDeleted.ToString() + " bản ghi!";
        BindData(-1);
    }

    protected void grvCity_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvCity.PageIndex = e.NewPageIndex;
        this.BindData(-1);
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindData(-1);
    }
}
